<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="真实姓名" prop="RealName">
        <el-input v-model="form.RealName" placeholder="请输入真实姓名"/>
      </el-form-item>
      <el-form-item label="性别" prop="Gender">
        <el-select v-model="form.Gender" placeholder="请选择性别">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="身份证号" prop="IDnumber">
        <el-input v-model="form.IDnumber" placeholder="请输入身份证号"/>
      </el-form-item>
      <el-form-item label="出生日期" prop="BirthDate">
        <el-date-picker v-model="form.BirthDate"
                        clearable
                        placeholder="请选择出生日期"
                        type="date"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年龄" prop="Age">
        <el-input v-model="form.Age" placeholder="请输入年龄"/>
      </el-form-item>
      <el-form-item label="家庭住址" prop="HomeAddress">
        <el-input v-model="form.HomeAddress" placeholder="请输入家庭住址"/>
      </el-form-item>
      <el-form-item label="病历号码"
                    prop="CaseNumber">
        <el-input v-model="form.CaseNumber"
                  placeholder="请输入病历号码" :disabled="true"/>
      </el-form-item>
      <el-form-item label="看诊日期" prop="VisitDate">
        <el-date-picker v-model="form.VisitDate"
                        clearable
                        placeholder="请选择看诊日期"
                        type="date"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="午别" prop="Noon">
        <el-select v-model="form.Noon" placeholder="请选择午别">
          <el-option
            v-for="dict in dict.type.noon"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="病历本" prop="IsBook">
        <el-switch v-model="isBook"/>
      </el-form-item>
      <el-form-item label="挂号时间" prop="RegisterTime">
        <el-date-picker v-model="form.RegisterTime"
                        clearable
                        placeholder="请选择挂号时间"
                        type="date"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="挂号员id" prop="RegisterId">
        <el-input v-model="form.RegisterId" placeholder="请输入挂号员id"/>
      </el-form-item>
      <el-form-item label="看诊状态" prop="VisitState">
        <el-select v-model="form.VisitState" placeholder="请选择看诊状态">
          <el-option
            v-for="dict in dict.type.regiser_state"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="挂号级别" prop="RegistLevelId">
        <el-input v-model="form.RegistLevelId" placeholder="请输入挂号级别" :disabled="true"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </div>
</template>

<script>
import {addRegister, caseNumber} from "@/api/system/Register";
import getNowFormatDate from "@/utils/getDate";

export default {
  name: "RegisterInfo",
  dicts: ['sys_user_sex', 'regiser_state', 'noon'],
  data() {
    return {
      isBook: false,
      form: {},
      rules: {
        RealName: [
          {required: true, message: "真实姓名不能为空", trigger: "blur"}
        ],
        Gender: [
          {required: true, message: "性别不能为空", trigger: "blur"}
        ],
        IDnumber: [
          {required: true, message: "身份证号不能为空", trigger: "blur"}
        ],
        BirthDate: [
          {required: true, message: "出生日期不能为空", trigger: "blur"}
        ],
        Age: [
          {required: true, message: "年龄不能为空", trigger: "blur"}
        ],
        HomeAddress: [
          {required: true, message: "家庭住址不能为空", trigger: "blur"}
        ],
        CaseNumber: [
          {
            required: true,
            message: "病历号码不能为空",
            trigger: "blur"
          }
        ],
        VisitDate: [
          {required: true, message: "看诊日期不能为空", trigger: "blur"}
        ],
        Noon: [
          {required: true, message: "午别不能为空", trigger: "change"}
        ],
        DeptId: [
          {required: true, message: "挂号科室id不能为空", trigger: "change"}
        ],
        UserId: [
          {required: true, message: "挂号医生id不能为空", trigger: "change"}
        ],
        RegisterTime: [
          {required: true, message: "挂号时间不能为空", trigger: "blur"}
        ],
        RegisterId: [
          {required: true, message: "挂号员id不能为空", trigger: "blur"}
        ],
        RegistLevelId: [
          {required: true, message: "挂号级别不能为空", trigger: "blur"}
        ]
      }
    };
  },
  watch: {
    isBook(val) {
      if (val === false) {
        this.form.isBook = 0;
      } else {
        this.form.isBook = 1;
      }
    }
  },
  async mounted() {
    this.reset();
    await caseNumber().then((res) => {
      this.form.CaseNumber = res;
      this.form.RegistLevelId = this.$route.query.registLevel;
      this.form.UserId = this.$route.query.userId;
      this.form.DeptId = this.$route.query.deptId;
      this.form.RegisterTime = getNowFormatDate();
      this.form.VisitDate = getNowFormatDate();
      this.form.Noon = new Date().getHours() < 12 ? 0 : 1;
      this.form.RegisterId = localStorage.getItem("userId");
    })
  },
  methods: {
    reset() {
      this.form = {
        Id: null,
        RealName: null,
        Gender: null,
        IDnumber: null,
        BirthDate: null,
        Age: null,
        HomeAddress: null,
        CaseNumber: null,
        VisitDate: null,
        Noon: null,
        DeptId: null,
        UserId: null,
        IsBook: 0,
        RegisterTime: null,
        RegisterId: null,
        VisitState: 1,
        RegistLevelId: null
      };
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.RegistLevelId = parseInt(this.$route.query.registLevelId || 1);
          this.form.RegisterId = parseInt((localStorage.getItem("userId")));
          addRegister(this.form).then(response => {
            this.$modal.msgSuccess("挂号成功");
            this.$router.push({
              path: '/registered/service'
            })
          });
        }
      });
    },
  }
};
</script>
